package xiong.rbac.view;

import dto.DeptDto;
import service.IDeptService;
import service.DeptService;
import vo.Result;
import vo.DeptVo;

public class DeptView extends BaseView<DeptVo, IDeptService> {
    public static final String MENU = """
            --------------
                部门管理
            --------------
            |  0 返回上级  |
            |  1 部门列表  |
            |  2 创建部门  |
            |  3 更新部门  |
            |  4 删除部门  |
            --------------
            请输入编号:""";
    /**
     * 视图对象缓存数据
     */
    private static DeptView singleton;

    private DeptView() {
        Service = DeptService.getInstance();
    }

    public static DeptView getInstance() {
        if (singleton == null) {
            singleton = new DeptView();
        }
        return singleton;
    }

    public void show() {
        String menuChoice;
        do {
            System.out.print(MENU);
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("已返回上一级菜单");
                case "1" -> list();
                // T000  部门列表
                case "2" -> create();
                // T000  创建部门
                case "3" -> update();
                // T000  更新部门
                case "4" -> delete();
                // T000  新建部门
                default -> System.out.println("输入错误,请重新输入");
            }
        } while (!"0".equals(menuChoice));
    }

    //ctrl+f(Find)
    //ctrl+r(Replace)
    //TODO: 查看部门列表，后续完成
    public void list() {
        Result result = Service.list();
        if (result.success && Result.data instanceof DeptVo[]) {
            cache = (DeptVo[]) Result.data;
            System.out.println("ID\t部门名称\t部门电话");
            for (DeptVo dept : cache) {
                System.out.printf("%d\t%s\t%s\n", dept.id, dept.name, dept.phone);
            }
        }
    }

    public void create() {
        System.out.print("请输入部门名称：");
        String name = MainView.input.nextLine();
        System.out.print("请输入部门电话：");
        String phone = MainView.input.nextLine();
        DeptDto dto = new DeptDto(name, phone);

        Result result = Service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }
        System.out.print("请输入部门名称，直接按回车表示不修改:");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? dept.name : name;

        System.out.print("请输入部门电话，直接按回车表示不修改:");
        String phone = MainView.input.nextLine();
        phone = "".equals(phone) ? dept.phone : phone;

        DeptDto dto = new DeptDto(name, phone);

        Result result = Service.update(dto, dept.id);

        if (!result.success) {
            System.out.println(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public void delete() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }
        System.out.print("输入'Y'确认删除，输入其他则取消删除:");
        String confirm = MainView.input.nextLine();

        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消删除");
            return;
        }

        Result result = Service.delete(dept.id);
        if (!result.success) {
            System.out.println(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }
}





